package com.tjut.core.tjut;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import com.tjut.core.CookieManager;
import com.tjut.core.LoginExecutor;
import com.tjut.core.utils.OkhttpUtils;
import com.tjut.core.utils.SaltUtil;
import okhttp3.Cookie;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TjutLogin extends LoginExecutor {


    public TjutLogin(CookieManager cookieManager) {
        super(cookieManager);
    }

    /**
     * 从网址index中获取string字符串
     *
     * @return 返回index的html字符串
     */
    private static String getIndexHTML() {
        return OkhttpUtils.get(TjutUrl.LOGIN_INDEX);
    }
    @Override
    public List<Cookie> active(Map params) {
        String html = getIndexHTML();
        Document doc = Jsoup.parse(html);
        String salt = getSalt(doc);
        if (salt.isEmpty()) return Collections.emptyList();
        Map<String, String> passwordPar = getPasswordPar(doc);
        passwordPar.put("username", String.valueOf(params.get("username")));
        try {
            passwordPar.put("password", SaltUtil.getPassword(String.valueOf(params.get("password")), salt));
        } catch (Exception e) {
            return Collections.emptyList();
        }
        String LOGIN_POST_STEP_1_URL = OkhttpUtils.post(TjutUrl.LOGIN_POST_STEP_1, passwordPar);
        //开始重定向
        assert (!LOGIN_POST_STEP_1_URL.isEmpty());
        String LOGIN_GET_STEP_2_URL = OkhttpUtils.get(LOGIN_POST_STEP_1_URL);
        assert (!LOGIN_GET_STEP_2_URL.isEmpty());
        String LOGIN_GET_STEP_3_URL = OkhttpUtils.get(LOGIN_GET_STEP_2_URL);
        assert (!LOGIN_GET_STEP_3_URL.isEmpty());
        OkhttpUtils.get(LOGIN_GET_STEP_3_URL);
        return cookieManager.getCookies();
    }
    private String getSalt(Document doc) {
        return doc.selectXpath("//*[@id=\"pwdDefaultEncryptSalt\"]").attr("value");
    }
    private Map<String, String> getPasswordPar(Document doc) {
        String lt = doc.selectXpath("//*[@id=\"casLoginForm\"]/input[1]").attr("value");
        String dllt = doc.selectXpath("//*[@id=\"casLoginForm\"]/input[2]").attr("value");
        String execution = doc.selectXpath("//*[@id=\"casLoginForm\"]/input[3]").attr("value");
        String _eventId = doc.selectXpath("//*[@id=\"casLoginForm\"]/input[4]").attr("value");
        String rmShown = doc.selectXpath("//*[@id=\"casLoginForm\"]/input[5]").attr("value");
        HashMap<String, String> map = new HashMap<>();
        map.put("lt", lt);
        map.put("dllt", dllt);
        map.put("execution", execution);
        map.put("_eventId", _eventId);
        map.put("rmShown", rmShown);
        return map;
    }

    @Override
    public boolean check() {
        String jsonString = OkhttpUtils.get(TjutUrl.USER_INFO_TEST_URL);
        Map<String, Object> map = JSON.parseObject(jsonString, new TypeReference<Map<String, Object>>() {});


        return true;
    }
}
